package com.broada.apm.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 读取系统的配置文件，集中提供系统配置信息。
 */
public class ConfigureProperties {
	private static Logger logger = LoggerFactory.getLogger(ConfigureProperties.class);
	private static Properties pro;

	static {
		loadPropertyFiles();
	}

	private static void loadPropertyFiles() {
		pro = new Properties();
		String configFilePath = System.getProperty("apm.config.file", "conf/configure.properties");
		File filePath = ApmFileUtils.getFile(configFilePath);

		InputStreamReader ins = null;
		try {
			ins = new InputStreamReader(new FileInputStream(filePath), "UTF-8");
			pro.load(ins);
		} catch (FileNotFoundException e) {
			if (logger.isErrorEnabled()) {
				logger.error("找不到系统配置文件！", e);
			}
		} catch (IOException e) {
			if (logger.isErrorEnabled()) {
				logger.error("配置文件解析错误！", e);
			}
		}
	}

	public static String getString(String key) {
		return pro.getProperty(key);
	}

	public static String getString(String key, String defaultVal) {
		return pro.getProperty(key, defaultVal);
	}
	public static int getInt(String key, int def) {
	    return pro.containsKey(key)? Integer.parseInt(pro.getProperty(key,"0")): def;
	}
}
